var util = require('./sever.js');
var network = require('./network.js');

function request(url, params, success, fail) {
  this.requestLoading(url, params, "", token, success, fail)
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
//token：用户token
function requestLoading(url, params, message, token, success, fail) {
  wx.showNavigationBarLoading()
  // wx.hideTabBar()
  if (message != "") {
    wx.showLoading({
      mask: true,
      title: message,
    })
  }
  wx.request({
    url: url,
    data: params,
    header: {
      //'Content-Type': 'application/json'
      'content-type': 'application/x-www-form-urlencoded',
      "authorization": token
    },
    method: 'post',
    success: function(res) {
      //console.log(res.data)
      wx.hideNavigationBarLoading()
      wx.showTabBar()
      if (message != "") {
        wx.hideLoading()
      }
      if (res.data.code == 200) {


        // var pages = getCurrentPages() //获取加载的页面
        // var currentPage = pages[pages.length - 1] //获取当前页面的对象
        // var urll = currentPage.route + "" //当前页面url
        // urll = urll.split("/")
        // util.setToken("mypage", "../" + urll[1] + "/" + urll[2])


        success(res.data)
      } else if (res.data.code == 300) {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        var urll = currentPage.route + "" //当前页面url
        urll = urll.split("/")
        util.setToken("mypage", "../" + urll[1] + "/" + urll[2])
        wx.switchTab({
          url: '../index/index'
        })
      } else if (res.data.code == 400) {
        wx.showToast({
          icon: "none",
          title: res.data.msg,
        })
      } else if (res.data.code == 301) {
        wx.navigateTo({
          url: '../IsNo/IsNo'
        })
      } else if (res.data.code == 303) {
        wx.showToast({
          icon: "none",
          title: res.data.msg,
        })
      } else {
        fail()
      }

    },
    fail: function(res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function(res) {

    },
  })
}
module.exports = {
  request: request,
  requestLoading: requestLoading
}